package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService replyService;

    @Resource
    private IArticleService articleService;

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId){
        //根据帖子id查询帖子详情，检验帖子是否有效
        Article article = articleService.selectById(articleId);
        //帖子是否存在
        if(article==null || article.getDeleteState()==1){
            //帖子不存在
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //帖子是否有效
        if(article.getState()==1){
            //无效
            return AppResult.failed("帖子已封贴");
        }
        //调用service
        List<ArticleReply> articleReplies = replyService.selectByArticleId(articleId);
        if(articleReplies==null){
            articleReplies=new ArrayList<>();
        }
        //返回结果
        return AppResult.success(articleReplies);
    }

    @ApiOperation("发布回复")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复内容") @RequestParam("content") @NonNull String content){
        //获取session对象
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //校验用户状态
        if(user.getState()==1){
            //用户已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子信息
        Article article = articleService.selectById(articleId);
        //校验帖子是否删除
        if(article==null || article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //校验帖子是否封贴
        if(article.getState()==1){
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_STATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_STATE));
        }
        //构造参数
        ArticleReply articleReply=new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        //调用service
        replyService.create(articleReply);
        //返回结果
        return AppResult.success("回复成功");
    }
}
